package com.app;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;

import com.app.R;

public class AniView extends View {
	private static String	logName	= "AniView";
	private Paint			paint	= null;
	////////////////////////////////
	private Bitmap			bmp		= null;
	public static AniView	ANIVIEW	= null;

	public void initView() {
		Log.e(logName, "构造");
		///////////////////////////////////
		//		this.setKeepScreenOn(true);
		//		this.setFocusable(true);
		//		this.setFocusableInTouchMode(true);
		///////////////////////////////////
		paint = new Paint();
		paint.setColor(Color.RED);
		paint.setTextSize(20);
		///////////////////////////////////
		bmp = BitmapFactory.decodeResource(getResources(), R.drawable.icon);
		ANIVIEW = this;
	}

	public AniView(Context context) {
		super(context);
		initView();
	}

	public AniView(Context context, AttributeSet attrs) {
		super(context, attrs);
		initView();
	}

	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		canvas.drawColor(Color.BLACK);
		canvas.drawText("View", 100, 50, paint);
		canvas.drawBitmap(bmp, 100, 100, paint);
	}

	
	private static int count=0;
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		if(event.getAction()==MotionEvent.ACTION_DOWN){
			count++;
			if(count>3)
				count=0;
			doAni(count);
		}
		return true;
	}
	private Animation	mAlphaAnimation;
	private Animation	mScaleAnimation;
	private Animation	mTranslateAnimation;
	private Animation	mRotateAnimation;

	public void doAni(int type){
		switch (type) {
			case 0:
				if (mAlphaAnimation == null)
					mAlphaAnimation = new AlphaAnimation(1.0f, 0.0f);
				mAlphaAnimation.setDuration(5000);
				this.startAnimation(mAlphaAnimation);
				break;
			case 1:
				if (mScaleAnimation == null)
					mScaleAnimation = new ScaleAnimation(0.0f, 1.0f, 0.0f, 1.0f, Animation.RELATIVE_TO_PARENT, 0.5f, Animation.RELATIVE_TO_PARENT, 0.0f);
				mScaleAnimation.setDuration(5000);
				this.startAnimation(mScaleAnimation);
				break;
			case 2:
				if (mTranslateAnimation == null)
					mTranslateAnimation = new TranslateAnimation(0, 100, 0, 100);
				mTranslateAnimation.setDuration(5000);
				this.startAnimation(mTranslateAnimation);
				break;
			case 3:
				if (mRotateAnimation == null)
					mRotateAnimation = new RotateAnimation(0.0f, 360.0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
				mRotateAnimation.setDuration(5000);
				this.startAnimation(mRotateAnimation);
				break;
		}
	}
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		Log.i(logName, "KEY=" + keyCode);
		switch (keyCode) {
			case KeyEvent.KEYCODE_DPAD_UP:
				if (mAlphaAnimation == null)
					mAlphaAnimation = new AlphaAnimation(1.0f, 0.0f);
				mAlphaAnimation.setDuration(5000);
				this.startAnimation(mAlphaAnimation);
				break;
			case KeyEvent.KEYCODE_DPAD_DOWN:
				if (mScaleAnimation == null)
					mScaleAnimation = new ScaleAnimation(0.0f, 1.0f, 0.0f, 1.0f, Animation.RELATIVE_TO_PARENT, 0.5f, Animation.RELATIVE_TO_PARENT, 0.0f);
				mScaleAnimation.setDuration(5000);
				this.startAnimation(mScaleAnimation);
				break;
			case KeyEvent.KEYCODE_DPAD_LEFT:
				if (mTranslateAnimation == null)
					mTranslateAnimation = new TranslateAnimation(0, 100, 0, 100);
				mTranslateAnimation.setDuration(5000);
				this.startAnimation(mTranslateAnimation);
				break;
			case KeyEvent.KEYCODE_DPAD_RIGHT:
				if (mRotateAnimation == null)
					mRotateAnimation = new RotateAnimation(0.0f, 360.0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
				mRotateAnimation.setDuration(5000);
				this.startAnimation(mRotateAnimation);
				break;
		}

		return super.onKeyDown(keyCode, event);
	}

}
